package com.dingding.es.config;


import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author liugongding
 * @description:
 * @date 2023/3/15 10:12 PM
 */

@Configuration
public class Config {

    @Value("${spring.elasticsearch.rest.host}")
    private String host;

    @Value("${spring.elasticsearch.rest.enable:true}")
    private boolean enable;

    @Value("${spring.elasticsearch.rest.port}")
    private int port;

    @Value("${spring.elasticsearch.rest.username}")
    private String username;

    @Value("${spring.elasticsearch.rest.password}")
    private String password;

    //注入IOC容器
    //@Bean
    //public ElasticsearchClient elasticsearchClient() throws Exception{
    //    ElasticsearchClient client = null;
    //    if (enable){
    //        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    //        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
    //        Path caCertificatePath = Paths.get("certs/http_ca.crt");
    //        CertificateFactory factory = CertificateFactory.getInstance("X.509");
    //        Certificate trustedCa;
    //        try(InputStream inputStream = Files.newInputStream(caCertificatePath)) {
    //            trustedCa = factory.generateCertificate(inputStream);
    //        }
    //        KeyStore trustStore = KeyStore.getInstance("pkcs12");
    //        trustStore.load(null, null);
    //        trustStore.setCertificateEntry("ca", trustedCa);
    //        SSLContextBuilder sslContextBuilder = SSLContexts.custom().loadTrustMaterial(trustStore, null);
    //        SSLContext sslContext = sslContextBuilder.build();
    //        RestClient restClient = RestClient.builder(new HttpHost(host, port))
    //                .setHttpClientConfigCallback(httpClientBuilder ->
    //                        httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)
    //                                .setSSLContext(sslContext)
    //                )
    //                .build();
    //        ElasticsearchTransport transport = new RestClientTransport(restClient,new JacksonJsonpMapper());
    //        client = new ElasticsearchClient(transport);
    //    }
    //    return client;
    //}

    @Bean
    public ElasticsearchClient client(){
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        //设置账号密码
        credentialsProvider.setCredentials(
                AuthScope.ANY, new UsernamePasswordCredentials(username, password));
        RestClient restClient = RestClient.builder(new HttpHost(host, port))
                .setHttpClientConfigCallback(httpClientBuilder->httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)).build();

        ElasticsearchTransport transport = new RestClientTransport(restClient,new JacksonJsonpMapper());
        return new ElasticsearchClient(transport);
    }

    //@Bean
    //public ElasticsearchClient getElasticsearchClient() {
    //    RestClient restClient = RestClient.builder(
    //            new HttpHost(host, port, "http")).build();
    //    ElasticsearchTransport transport = new RestClientTransport(
    //            restClient, new JacksonJsonpMapper());
    //    return new ElasticsearchClient(transport);
    //}
}
